package com.example.demo.base;

import com.baomidou.mybatisplus.annotations.TableField;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;

public class FieldHandlerUtils {

    private static Logger logger = LoggerFactory.getLogger(FieldHandlerUtils.class);

    private static final String FIELD_DELETED = "deleted";

    public static <T> boolean hasDeleteField(Class<T> tClass){
        try {
            Field field = FieldUtils.getField(tClass, FIELD_DELETED, true);
            if (field != null) {
                boolean isTableFieldAno=field.isAnnotationPresent(TableField.class);
                if(isTableFieldAno){
                    TableField tableField=field.getAnnotation(TableField.class);
                    if(tableField!=null&&!tableField.exist()){
                        return false;
                    }
                }
                return true;
            }
        } catch (IllegalArgumentException ie) {
        } catch (Exception e) {
            logger.error("类{}校验deleted字段失败", tClass.getName(),e);
        }
        return false;
    }
}
